/*
 * @Description:
 * @Author: fhw
 * @Date: 2023-02-08 10:05:40
 */
import { ApiProperty } from '@nestjs/swagger';
import { Exclude } from 'class-transformer';
import { BaseEntity, PaginatedResult } from 'src/common/entities/entities';
import {
  BeforeInsert,
  Column,
  Entity,
  JoinColumn,
  ManyToOne,
  OneToMany,
  OneToOne,
  PrimaryGeneratedColumn,
} from 'typeorm';
import { hashSync } from 'bcryptjs';
import { Status } from 'src/dictionaries/index.enum';
import { FeedbackEntity } from '../../feedback/entities/feedback.entity';
import { OrderEntity } from '../../order/entities/order.entity';
@Entity('user')
export class UserEntity extends BaseEntity {
  @ApiProperty({ example: 'id', description: 'id' })
  @PrimaryGeneratedColumn({
    type: 'int',
    comment: 'id',
  })
  id: number;
  /** 用户名称 */
  @ApiProperty({ example: 'username', description: '用户名称' })
  @Column({
    type: 'varchar',
    comment: '用户名称',
    default: '新用户',
    length: 50
  })
  username: string;

  /** 密码 */
  @ApiProperty({ example: 'password', description: '密码' })
  @Exclude()
  @Column({
    type: 'varchar',
    length: 100,
    comment: '密码',
    select: false,
  })
  password: string;

  @BeforeInsert()
  async encryptPwd() {
    this.password = await hashSync(this.password, 10);
  }

  /** 用户头像 */
  @ApiProperty({ example: 'avatar', description: '用户头像' })
  @Column({
    type: 'varchar',
    comment: '用户头像',
    nullable: true,
  })
  avatar: string;
  /** 性别 1:男性，2:女性*/
  @ApiProperty({
    example: 'gender',
    description: '性别 1:男性，2:女性',
  })
  @Column({
    comment: '性别 1:男性，2:女性',
    type: 'enum',
    enum: [1, 2],
    nullable: true
  })
  gender: string;

  /** 联系QQ */
  @ApiProperty({ example: 'QQ', description: '联系QQ' })
  @Column({
    type: 'varchar',
    comment: '联系QQ',
    nullable: true,
  })
  QQ: string;

  /** 电话号码 */
  @ApiProperty({ example: 'mobile', description: '电话号码' })
  @Column({
    type: 'varchar',
    comment: '电话号码',
  })
  mobile: string;

  /** 邮箱 */
  @ApiProperty({ example: 'email', description: '邮箱' })
  @Column({
    type: 'varchar',
    comment: '邮箱',
    nullable: true,
  })
  email: string;

  /** 个人介绍 */
  @ApiProperty({ example: 'description', description: '个人介绍' })
  @Column({
    type: 'varchar',
    comment: '个人介绍',
    nullable: true
  })
  description: string;

  /** 访问时间 */
  @ApiProperty({ example: 'accessDate', description: '访问时间' })
  @Column({
    type: 'datetime',
    comment: '访问时间',
    nullable: true,
  })
  accessDate: Date;

  /** 生日字段 */
  @ApiProperty({ example: 'birthdayDate', description: '生日字段' })
  @Column({ type: 'date', nullable: true })
  birthdayDate: string | null;

  /** 用户状态 */
  @ApiProperty({
    example: 'status',
    description: '用户状态',
    default: Status.ENABLED,
  })
  @Column({
    type: 'enum',
    comment: '用户状态',
    enum: Status,
    default: Status.ENABLED,
  })
  status: Status;

  /** 评论点赞 */
  @OneToMany(() => FeedbackEntity, (feedback) => feedback.userId)
  feedback: FeedbackEntity[];

  @OneToMany(() => OrderEntity, (order) => order.user)
  order?: OrderEntity[];


}
export class UserResult extends PaginatedResult {
  @ApiProperty({ example: 'data', description: 'data' })
  data: UserEntity;
}